﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Xml;
using ServBus.ListForm2013.Utility;
using System.IO;

namespace ServBus.ListForm2013.ListFormWebPart
{
    [ToolboxItemAttribute(false)]
    public class ListFormWebPart : WebPart
    {
        // 当更改可视 Web 部件项目项时，Visual Studio 可能会自动更新此路径。
        //private const string AscxPath = @"~/_CONTROLTEMPLATES/ServBus.ListForm2013/ListFormWebPart/ListFormWebPartUserControl.ascx";
        private const string AscxPath = @"~/_CONTROLTEMPLATES/15/ServBus.ListForm2013/ListFormWebPart/ListFormWebPartUserControl.ascx";

        protected override void CreateChildControls()
        {
            ListFormWebPartUserControl control = Page.LoadControl(AscxPath) as ListFormWebPartUserControl;
            if (control != null)
            {
                control.webpart = this;
            }
            if (control != null) Controls.Add(control);
        }


        #region Properties
        private bool _showRibbon = true;
        [WebBrowsable(true)]
        [WebDescription("ShowRibbon")]
        [WebDisplayName("ShowRibbon")]
        [Personalizable(PersonalizationScope.Shared)]
        public bool ShowRibbon
        {
            get { return _showRibbon; }
            set
            { _showRibbon = value;
            }
        }

        #endregion

        public override EditorPartCollection CreateEditorParts()
        {

            EditorPartCollection baseParts = base.CreateEditorParts();

            List<EditorPart> editorParts = new List<EditorPart>(1);

            EditorPart part = new MyEditorPart();

            part.ID = ID + "_tagValueEditor";

            part.Title = @"InfoPath Form";

            editorParts.Add(part);

            return new EditorPartCollection(baseParts, editorParts);

        }
    }



    #region EditorPart

    public class MyEditorPart : EditorPart
    {
        private CheckBox _cbShowRibbon;

        private readonly Dictionary<string, string> _urlParamDic = new Dictionary<string, string>();
        

        private string Request(string param)
        {
            string returnValue = string.Empty;

            if (_urlParamDic.ContainsKey(param.ToUpper()))
            {
                returnValue = _urlParamDic[param.ToUpper()];
            }
            return returnValue;
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            
            _cbShowRibbon = new CheckBox {Text = @"Show InfoPath Ribbon or toolbar<br />"};
            Label alterLine = new Label {Text = @"<div style='width:100%' class='UserDottedLine'></div>"};
            
            Controls.Add(_cbShowRibbon);
            Controls.Add(alterLine);
        }

       
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            ListFormWebPart webPart = WebPartToEdit as ListFormWebPart;
            if (webPart != null)
            {
                webPart.ShowRibbon = _cbShowRibbon.Checked;

            }
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            ListFormWebPart webPart = WebPartToEdit as ListFormWebPart;
            if (webPart != null)
            {
                _cbShowRibbon.Checked = webPart.ShowRibbon;
            }
        }
    }
    #endregion
}
